package com.ptszyxx.popose.module.chat.page.vm;

import android.app.Application;
import androidx.annotation.NonNull;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.EditEnum;
import com.ptszyxx.popose.common.event.ChatClearMessageEvent;
import com.ptszyxx.popose.common.event.EditEvent;
import com.ptszyxx.popose.common.utils.YVipUtil;
import com.ptszyxx.popose.common.utils.sensitive.YSensitiveUtil;
import com.ptszyxx.popose.module.main.gift.vm.GiftAndCallVM;
import com.tencent.qcloud.tuikit.tuichat.zhiyu.callback.BooleanCallback;
import com.tencent.qcloud.tuikit.tuichat.zhiyu.callback.ExpressionMessageCallback;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnErrorResult;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.expression.ExpressionEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YChannelUtil;
import com.ysg.utils.YDbUtils;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ChatVM extends GiftAndCallVM {
    public String txUserId, userName, userPic;
    public UserResult userEntity;
    public List<ExpressionEntity> expressionList = new ArrayList<>();

    public ChatVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onUserInfoEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onClearMessageEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onExpressionDialogEvent = new SingleLiveEvent<>();
    }

    public Disposable disposable, disposableExpression;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(ChatClearMessageEvent.class)
                .subscribe(new Consumer<ChatClearMessageEvent>() {
                    @Override
                    public void accept(ChatClearMessageEvent event){
                        if (event != null){
                            uc.onClearMessageEvent.call();
                        }
                    }
                });
        disposableExpression = RxBus.getDefault().toObservable(EditEvent.class)
                .subscribe(new Consumer<EditEvent>() {
                    @Override
                    public void accept(EditEvent event){
                        if (event != null && event.getEditEnum() != null){
                            if (EditEnum.expression == event.getEditEnum()){
                                requestExpression(false);
                            }
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
        RxSubscriptions.remove(disposableExpression);
    }


    //=========================================接口：start===========================================
    /**
     * 请求用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();
        map.put("userid", txUserId);

        HttpUtils.getInstance()
                .data(model.userInfo(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                userEntity = response.getData();
                                userName = userEntity.getNick();
                                uc.onUserInfoEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 扣费：文本
     */
    public void requestTextFee(String content, BooleanCallback callback, ExpressionMessageCallback expressionMessageCallback){
        if (!YVipUtil.isChatVip()){
            baseUC.onNoDiamondEvent.call();
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("text", content);//敏感词
        map.put("txCode", txUserId);
        HttpUtils.getInstance()
                .data(model.textFee(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
                        if (callback != null) callback.onSuccess();
                        if (expressionMessageCallback != null){
                            expressionMessageCallback.onSendExpression(content);
                        }
                        YDbUtils.getInstance().saveChatCount();
                    }
                }, new OnErrorResult() {
                    @Override
                    public void onErrorResult(int code, String msg) {
                        baseUC.onNoDiamondEvent.call();
                    }
                });
    }

    /**
     * 聊天常用语
     */
    public void requestExpression(boolean showDialog){
        if (YChannelUtil.getInstance().beta()){
            expressionList = YDbUtils.getInstance().findExpression();
            if (showDialog){
                uc.onExpressionDialogEvent.call();
            }
        }else{
            Map<String, String> map = new HashMap<>();
            map.put("sex", YSPUtils.getInstance().getUserSex());
            HttpUtils.getInstance()
                    .list(model.expressionList(map), this, new OnSuccessListResult<ExpressionEntity>() {
                        @Override
                        public void onSuccessResult(BaseListResponse<ExpressionEntity> response) {
                            expressionList = response.getData();
                            if (showDialog){
                                uc.onExpressionDialogEvent.call();
                            }
                        }
                    });
        }
    }
    /**
     * 聊天常用语
     */
    public void requestExpressionDelete(String id){
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        HttpUtils.getInstance()
                .list(model.expressionDelete(map), this, new OnSuccessListResult<ExpressionEntity>() {
                    @Override
                    public void onSuccessResult(BaseListResponse<ExpressionEntity> response) {
                        YToastUtil.showShort(R.string.success_delete);
                        requestExpression(false);
                    }
                });
    }

    //=========================================接口：end=============================================
    /**
     * 记录敏感词
     */
    public void saveSensitive(String input, String sensitiveData){
        YSensitiveUtil.save(this, model, input, sensitiveData);
    }




}
